<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class RegistrationApiController extends AbstractRestfulController
{
    public function getList()
    {			
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');		
		$getUserlist = $userTable->getAdminlist();	
		if(($getUserlist->count())!=0){
		$data = array();$i=0;
				foreach($getUserlist as $users){
					$uid=$users->user_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['user_name']= $users->user_name;
					$data[$i]['user_email']= $users->user_email;
					$data[$i]['user_mobile']= $users->user_mobile;
					$data[$i]['action']='<a href="javascript:void(0)" onclick = "editUserFunction('.$uid.',1)">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteuser('.$uid.')">Delete</a>';
					$i++;
				}
				$data['aaData'] = $data;
				echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}		
    }
    public function get($user_id)
    {		
	
		header('Access-Control-Allow-Origin: *');
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');	
		$userData = $userTable->getUserData($user_id)->toarray();
		if(count($userData)!=0){
			return new JsonModel(array(
				'data' 	=> json_encode($userData),
				'value'	=> 1,
			));
		}else{
			return new JsonModel(array(
				'output' 	=> 'no data',
				'value' 	=> 0,
			));
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');	
		if($data['hid_user_id']==0){
			$checkUser = $userTable->checkUniqueRecord($data['user_email'])->toarray();	
		}else{
			$updateUser= $userTable->updateUserData($data);
			return new JsonModel(array(
				'output' 	=> 	'success',
				'UserId'	=>	$updateUser,
				'value' 	=> 	1,
			));
		}
		if(count($checkUser)==0){
			global $registrationConfirmationSubject;
			global $registrationConfirmationMessage;
			$mailinfo=array();	
			$email=$data['user_email'];			
			$password=$data['user_password'];			
			$module="http://aapthitech.com/demos/vtphtmls/menu.html?id=";
			$userRegistration = $userTable->saveUserData($data);
			if($userRegistration!=0){
				$username=ucfirst($data['user_name']);
				$registrationConfirmationMessage = str_replace("<FULLNAME>","$username", $registrationConfirmationMessage);
				$registrationConfirmationMessage = str_replace("<EMAIL>","$email", $registrationConfirmationMessage);
				$registrationConfirmationMessage = str_replace("<PASSWORD>","$password", $registrationConfirmationMessage);
				$registrationConfirmationMessage = str_replace("<PASSWORDLINK>",$module.$userRegistration, $registrationConfirmationMessage);
				$to=$email;	
				if(sendMail($to,$registrationConfirmationSubject,$registrationConfirmationMessage)){
					return new JsonModel(array(
						'output' 	=> 	'success',
						'UserId'	=>	$userRegistration,
						'value' 	=> 	1,
						'message' 	=> 	"Email sent successfully",
					));
				}else{
					return new JsonModel(array(
						'output' 	=> 'not success',
						'value' 	=> 0,
						'message' 	=> 	"Email sent failed",
					));
				}	
			}else{
				return new JsonModel(array(
					'output' 	=> 'not success',
					'value' 	=> 0,
				));
			}
		}else{
			return new JsonModel(array(
				'output' 	=> 'not success',
				'value' 	=> 0,
				'data' 		=> 'Email already exists',
			));
		}
    }
    public function update($uid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UsersFactory');
		$userStatusUpdate = $userTable->userStatusUpdate($uid,$data);
		if($userStatusUpdate){
			return new JsonModel(array(
				'output' => 'success',
				'value' => 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}